package hn.cch.springboot_datasource.jpa;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "hn.cch.springboot_datasource.jpa.mysql",
        entityManagerFactoryRef = "mysqlEntityManagerFactoryBean",
        // transactionManagerRef = "mysqlJpaTransactionManager"
        transactionManagerRef = "mysqlTransactionManager"
)
public class MysqlJpaConfiguration {


    @Autowired
    @Qualifier("mysqlDataSource")
    private DataSource dataSource;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Primary
    @Bean(name = "mysqlEntityManagerFactoryBean")
    public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactoryBean(EntityManagerFactoryBuilder factoryBuilder) {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(false);
        //不同类型数据库配置数据库方言
        jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");

        LocalContainerEntityManagerFactoryBean factoryBean = factoryBuilder.dataSource(dataSource)
                // spring boot 1.5.x
                // .properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
                // spring boot 2.1.x
                .properties(hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings()))
                .packages("hn.cch.springboot_datasource.entity")
                .persistenceUnit("mysqlPersistenceUnit")
                .build();
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        return factoryBean;
    }

    @Primary
    @Bean(name = "mysqlEntityManager")
    public EntityManager mysqlEntityManager(EntityManagerFactoryBuilder factoryBuilder) {
        return mysqlEntityManagerFactoryBean(factoryBuilder).getObject().createEntityManager();
    }


    // @Primary
    // @Bean(name = "mysqlJpaTransactionManager")
    // public JpaTransactionManager mysqlJpaTransactionManager(EntityManagerFactoryBuilder factoryBuilder) {
    //     return new JpaTransactionManager(mysqlEntityManagerFactoryBean(factoryBuilder).getObject());
    // }

}
